package com.shengyuan.manage_os.ui.fragment;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.base.BaseFragment;
import com.shengyuan.manage_os.im.common.utils.ToastUtil;
import com.shengyuan.manage_os.im.ui.ContactListFragment;
import com.shengyuan.manage_os.im.ui.LocationActivity;
import com.shengyuan.manage_os.im.ui.settings.SettingsActivity;
import com.shengyuan.manage_os.old.ying.boce.jsy.PT_yhmc;
import com.shengyuan.manage_os.old.ying.erpm.About;
import com.shengyuan.manage_os.old.ying.erpm.Actual_OA_CarBaoXiao;
import com.shengyuan.manage_os.old.ying.erpm.Actual_OA_JiDongRenDengJi;
import com.shengyuan.manage_os.old.ying.erpm.Actual_OA_WorkDialy;
import com.shengyuan.manage_os.old.ying.erpm.Actual_System_Setting;
import com.shengyuan.manage_os.old.ying.erpm.Actual_WebView;
import com.shengyuan.manage_os.old.ying.erpm.GongChengActivity;
import com.shengyuan.manage_os.old.ying.erpm.JiXieActivity;
import com.shengyuan.manage_os.old.ying.erpm.LoginActivity;
import com.shengyuan.manage_os.old.ying.erpm.NetActivity;
import com.shengyuan.manage_os.old.ying.erpm.OAActivity;
import com.shengyuan.manage_os.old.ying.erpm.XingZhengActivity;
import com.shengyuan.manage_os.old.ying.erpm.util.SharePreferenceUtil;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;
import com.shengyuan.manage_os.utils.GridViewTools;

//import static com.shengyuan.manage_os.old.ying.erpm.SettingActivity.InitGoldelData;

/**
 * Created by Administrator on 2016/11/15.
 */

public class WorkFragment extends BaseFragment implements View.OnClickListener{

    private TextView system,about;
    private GridView gview,gridView;
//    private List<Contacts> dataList = new ArrayList<>();
    private static WorkFragment workFragment;
    private SharePreferenceUtil util;
    private View view;
    private GridViewTools gridViewTools = null;


    public WorkFragment(){
        super();
        init();
    }

    public  WorkFragment init(){
//        if(workFragment == null){
            transaction = manager.beginTransaction();
            workFragment = this;
            transaction.add(R.id.fragment_content,workFragment);
            transaction.commit();
//        }
        return workFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if(view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if(parent != null) {
                parent.removeView(view);
            }
        }
        view = inflater.inflate(R.layout.fragment_work, container, false);
        gview = (GridView) view.findViewById(R.id.app_item);
        gridView = (GridView) view.findViewById(R.id.control_app_item);
        system = (TextView) view.findViewById(R.id.system);
        about = (TextView) view.findViewById(R.id.about);
        system.setOnClickListener(this);
        about.setOnClickListener(this);
//        InitGoldelData();

        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        gridViewTools = null;
    }

    @Override
    public void onResume() {
        super.onResume();
        gridViewTools = new GridViewTools(getActivity());
        gridViewTools.setGviewData(gview);
        gridViewTools.setGviewData(gridView);
        util = SharePreferenceUtil.init(getActivity(),"SAVE_USER");
        GridViewTools.setStartActivityListener(new GridViewTools.StartActivityListener() {
            @Override
            public void start(Object view1, int item) {
                Intent intent = new Intent();
                if (LoginActivity.pmUser == null&&LoginActivity.pmUser.UserName.equals("")) {
                    intent.setClass( getActivity(), LoginActivity.class );
                    startActivity(intent);
                    Utility.MessageBox("您的账号出现异常，请重新登录");
                    return;

                }else if (LoginActivity.pmUser.getLogOut() == true) {
                    intent.setClass( getActivity(), LoginActivity.class );
                    startActivity(intent);
                    return;
                }
                if(((Adapter)view1).getCount() == 7)
                    switch (item){
                        case 0:
                            intent.setClass(getActivity(), OAActivity.class);
                            startActivity(intent);
                            break;
                        case 1:
                            intent.setClass(getActivity(), Actual_WebView.class);
                            startActivity(intent);
                            break;
                        case 2:
                            intent.setClass(getActivity(), Actual_OA_WorkDialy.class);
                            startActivity(intent);
                            break;
                        case 3:
                            intent.setClass(getActivity(), Actual_WebView.class);
                            startActivity(intent);
                            break;
                        case 4:
                            intent.setClass(getActivity(), Actual_OA_CarBaoXiao.class);
                            startActivity(intent);
                            break;
                        case 5:
                            intent.setClass(getActivity(), Actual_OA_JiDongRenDengJi.class);
                            startActivity(intent);
                            break;
                        case 6:
                            intent.setClass(getActivity(), LocationActivity.class);
                            startActivity(intent);
                            break;

                    }
                else
                    switch (item){
                        case 0:
                            intent.setClass(getActivity(), GongChengActivity.class);
                            startActivity(intent);
                            break;
                        case 1:
                            intent.setClass(getActivity(), JiXieActivity.class);
                            startActivity(intent);
                            break;
                        case 2:
                            intent.setClass(getActivity(), XingZhengActivity.class);
                            startActivity(intent);
                            break;
                        case 3:
                            intent.setClass(getActivity(), NetActivity.class);
                            startActivity(intent);
                            break;
                    }

            }
        });

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){
            case R.id.system:
                intent.setClass(getActivity(), About.class);
                startActivity(intent);
                break;
            case R.id.about:
                intent.setClass(getActivity(),Actual_System_Setting.class);
                startActivity(intent);
                break;
        }
    }
}
